package generic;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map.Entry;

import generic.Employee;
import generic.Predicate;

public class GenericApp {

    public void execute() {
        System.out.println("hello");

        Double[] result = swap(0, 1, 1.5, 2d, 3d);
        System.out.println(Arrays.toString(result));

        Employee employee1 = new Employee("Tom", 200);
        Employee employee2 = new Employee("Luccy", 100);
        Employee employee3 = new Employee("Jam", 300);
        Employee[] employees = new Employee[]{employee1, employee2, employee3};
        printAll(employees, e -> e.getSalary() > 100);
        printAll(employees, e -> e.toString().length() % 2 == 1);

    }


    public static <T> T[] swap(int i, int j, T... values) {
        T temp = values[i];
        values[i] = values[j];
        values[j] = temp;
        return values;
    }

    public static <T extends AutoCloseable> void closeAll(ArrayList<T> elems) 
        throws Exception {
        for(T elem : elems) elem.close();
    }

    // public static void printAll(Employee[] staff, Predicate<Employee> filter) {
    //     for (Employee e : staff) {
    //         if (filter.test(e))
    //             System.out.println(e.getName());
    //     }
    // }

    public static void printAll(Employee[] staff, Predicate<? super Employee> filter) {
        for (Employee e : staff) {
            if (filter.test(e))
                System.out.println(e.getName());
        }
    }
}

